/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.qx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.qx.entity.QxMatchHistory;
import com.thinkgem.jeesite.modules.qx.entity.QxTeam;
import com.thinkgem.jeesite.modules.qx.service.QxMatchHistoryService;
import com.thinkgem.jeesite.modules.qx.service.QxTeamService;

/**
 * 单表生成Controller
 * @author zequs
 * @version 2017-08-23
 */
@Controller
@RequestMapping(value = "${adminPath}/qx/qxMatchHistory")
public class QxMatchHistoryController extends BaseController {

	@Autowired
	private QxMatchHistoryService qxMatchHistoryService;
	@Autowired
	private QxTeamService qxTeamService;
	
	@ModelAttribute
	public QxMatchHistory get(@RequestParam(required=false) String id) {
		QxMatchHistory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = qxMatchHistoryService.get(id);
		}
		if (entity == null){
			entity = new QxMatchHistory();
		}
		return entity;
	}
	
	@RequiresPermissions("qx:qxMatchHistory:view")
	@RequestMapping(value = {"list", ""})
	public String list(QxMatchHistory qxMatchHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<QxMatchHistory> page = qxMatchHistoryService.findPage(new Page<QxMatchHistory>(request, response), qxMatchHistory); 
		model.addAttribute("page", page);
		return "modules/qx/qxMatchHistoryList";
	}
	@RequiresPermissions("qx:qxMatchHistory:view")
	@RequestMapping(value = {"sixMatch", ""})
	public String history(QxMatchHistory qxMatchHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		qxMatchHistory.setIslimit(true);
		if (StringUtils.isNotBlank(qxMatchHistory.getTeamid())) {
			
		}
		QxTeam qxTeam = qxTeamService.get(qxMatchHistory.getTeamid());
		model.addAttribute("qxTeam", qxTeam);
		List<QxMatchHistory> list = qxMatchHistoryService.findList(qxMatchHistory);
//		Page<QxMatchHistory> page = qxMatchHistoryService.findPage(new Page<QxMatchHistory>(request, response), qxMatchHistory); 
		qxMatchHistory.setHistories(list);
		model.addAttribute("qxMatchHistory", qxMatchHistory);
		return "modules/qx/qxMatchHistorySixMatch";
	}

	@RequiresPermissions("qx:qxMatchHistory:view")
	@RequestMapping(value = "form")
	public String form(QxMatchHistory qxMatchHistory, Model model) {
		model.addAttribute("qxMatchHistory", qxMatchHistory);
		return "modules/qx/qxMatchHistoryForm";
	}

	@RequiresPermissions("qx:qxMatchHistory:edit")
	@RequestMapping(value = "save")
	public String save(QxMatchHistory qxMatchHistory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, qxMatchHistory)){
			return form(qxMatchHistory, model);
		}
		qxMatchHistoryService.save(qxMatchHistory);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxMatchHistory/?repage";
	}
	
	@RequiresPermissions("qx:qxMatchHistory:edit")
	@RequestMapping(value = "delete")
	public String delete(QxMatchHistory qxMatchHistory, RedirectAttributes redirectAttributes) {
		qxMatchHistoryService.delete(qxMatchHistory);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/qx/qxMatchHistory/?repage";
	}

}